package activator;

import java.util.Dictionary;

import library.service.LibraryService;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import com.prosyst.util.ref.Log;

import service.impl.FictionLibraryServiceImpl;

public class FictionLibraryImplementorActivator implements BundleActivator {

	private ServiceRegistration libraryServiceRegistration;
	private Log log;

	// --- Implementation of BundleActivator interface -------------------------

	public void start(BundleContext context) throws Exception {

		// deliver serviceProperties
		Dictionary serviceProperties = ServicePropertiesProvider
				.getServiceProperties();

		// register libraryService
		libraryServiceRegistration = context.registerService(
				LibraryService.class.getName(),
				new FictionLibraryServiceImpl(), serviceProperties);

		// initialize log (and reuse system log-configuration)
		log = new Log(context);
		log.setLogLevel(Log.INFO);
		log.setPrintLevel(Log.INFO);

		// throw log message
		log.info("Fiction LibraryService REGISTERED");
	}

	public void stop(BundleContext context) throws Exception {
		// cleanup libraryServiceRegistration
		if (libraryServiceRegistration != null) {
			libraryServiceRegistration.unregister();
			libraryServiceRegistration = null;

			log.info("Fiction LibraryService UN-REGISTERED");
		}

		// log clean up
		log.close();
		log = null;
	}
}
